package org.test.client.ui.component;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.test.client.listener.ClickAction;
import org.test.client.ui.MainPanel;
import org.test.client.ui.component.btn.LinkButton;
import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIConstants;

public class BreadcrumbNav extends JPanel{
	
	private MainPanel main = null;
	
	private BreadcrumbNav thisNav = this;
	
	public static Color back = Icons.getColor("f5f5f5");
	public static Color fontColor = Icons.getColor("2fa4e7");
	public static Color sepColor = Icons.getColor("cccccc");
	
	private Vector<LinkButton> navBtns = new Vector<LinkButton>(); 
	private Vector<JLabel> labels = new Vector<JLabel>(); 
	private LinkButton homeBtn = new LinkButton("Home");
	
	public BreadcrumbNav(MainPanel main) {
		this.main = main;
//		this.setBackground(back);
		this.setPreferredSize(new Dimension(0,35));
		navBtns.add(homeBtn);
		
		LinkButton homeBtn2 = new LinkButton("DashBoard");
		navBtns.add(homeBtn2);
	}
	
	public BreadcrumbNav doInit(){
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.add(Box.createHorizontalStrut(UIConstants.leftMenuAndMainGap));
		int size = navBtns.size();
		for(int i=0;i<size;i++){
			LinkButton linkButton = navBtns.get(i);
			this.add(linkButton);
			if(i !=size-1){
				JLabel sepLabel = createSepLabel();
				labels.add(sepLabel);
				this.add(sepLabel);
			}
		}
		return this;
	}
	
	@Override
	protected void paintChildren(Graphics g) {
		int www = this.getWidth();
		int hhh = this.getHeight();
		
		g.setColor(back);
		g.fillRect(UIConstants.leftMenuAndMainGap, 0, www-UIConstants.leftMenuAndMainGap, hhh);
		
		super.paintChildren(g);
	}
	
	public JLabel createSepLabel(){
		
		JLabel sep = new JLabel("/");
		sep.setForeground(sepColor);
		sep.setFont(UIConstants.commonFont);
		
		return sep;
	}
	
	public void addNavBtn(LinkButton b){
		navBtns.add(b);
		this.add(createSepLabel());
		this.add(b);
		this.revalidate();
	}
	
	public void removeLast(){
		LinkButton btn = navBtns.remove(navBtns.size()-1);
		JLabel sep = labels.remove(labels.size()-1);
		this.remove(btn);
		this.remove(sep);
		this.revalidate();
	}
	
	public void replaceLast(String key,ActionListener al){
		LinkButton remove = navBtns.remove(navBtns.size()-1);
		this.remove(remove);
		LinkButton newBtn = new LinkButton(key);
		navBtns.add(newBtn);
		if(al!=null){
			newBtn.addActionListener(al);
		}
		this.add(newBtn);
		this.revalidate();
	}
	
}
